from functools import wraps
from finance_c2c.local_settings import Config, ONLINE
from django.utils.decorators import available_attrs
from django.http import HttpResponseRedirect
from finance_c2c.local_settings import DEBUG


def wx_login(login_url="/mp/login"):
    def decorator(view_func):
        """django.contrib.auth.decorators重写"""

        @wraps(view_func, assigned=available_attrs(view_func))
        def _wrapped_view(request, *args, **kwargs):
            if not ONLINE:
                return view_func(request, *args, **kwargs)
            else:
                # if not ONLINE:  # 如果在测试服务器上
                #     return view_func(request, *args, **kwargs)
                path = request.get_full_path()
                # 获取账号
                if request.session.has_key(Config.session_key):
                    info = view_func(request, *args, **kwargs)
                    return info
                elif request.POST.get("account") or request.GET.get("account"):
                    info = view_func(request)
                    return info
                else:
                    return HttpResponseRedirect(login_url + '?next=%s' % path)

        return _wrapped_view

    return decorator
